/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.example.ui.component.button
{

import mx.managers.IFocusManagerComponent;

import org.example.ui.component.IButton;
import org.teotigraphix.dx.core.ToolKitUtil;
import org.teotigraphix.dx.interfaces.ISkinStateAware;
import org.teotigraphix.tka.patterns.component.ToolKitComponent;

import spark.components.supportClasses.TextBase;
import spark.primitives.BitmapImage;

/*

//--------------------------------------
//  Public API [mxml [interface] ]
//--------------------------------------

- IButton

  - content:*
  - label:String
  - icon:Class

//--------------------------------------
//  Behavior-Model Contract [interface]
//--------------------------------------

- IButtonModel extends IButton

  - hoveredOver:Boolean
  - keyboardPressed:Boolean
  - mouseCaptured:Boolean

//--------------------------------------
//  Core
//--------------------------------------

- [C] Button

- [M] ButtonModelCore
- [S] ButtonSkin
- [B] ButtonCoreBehavior

//--------------------------------------
//  Factory
//--------------------------------------

- ButtonToolKitFactor
  - creates ButtonModelCore
  - creates ButtonSkin
  - creates ButtonBehaviorCore

//--------------------------------------
//  Enumerations/Utility
//--------------------------------------

- ButtonState
  - enumerates button states
- ButtonStyles
  - initializes the TypeSelector with skinClass:ButtonSkin

*/

/**
 * Up State of the Button.
 */
[SkinState("up")]

/**
 * Over State of the Button.
 */
[SkinState("over")]

/**
 * Down State of the Button.
 */
[SkinState("down")]

/**
 * Disabled State of the Button.
 */
[SkinState("disabled")]

/**
 * ButtonBase is an abstract class, you need to set <code>toolKitFactory</code>
 * at the minimum.
 * 
 * @copyright Teoti Graphix, LLC
 * @author Michael Schmalle
 * @date 12-08-08
 */
public class ButtonBase
	   extends ToolKitComponent 
	   implements IFocusManagerComponent, IButton
{
	public var explicitToolTip:Boolean = false;
	
	//--------------------------------------------------------------------------
	//
	//  Public Skin Part :: Variables
	//
	//--------------------------------------------------------------------------
	
	[SkinPart(required="false")]
	
	/**
	 * A skin part that defines the label of the button.
	 */
	public var labelDisplay:TextBase;
	
	[SkinPart(required="false")]
	
	/**
	 * A skin part that defines the icon of the button.
	 */
	public var iconDisplay:BitmapImage;
	
	//--------------------------------------------------------------------------
	//
	//  Public :: Properties
	//
	//--------------------------------------------------------------------------
	
//	public var truncatedToolTipEnabled:Boolean;
	
	//----------------------------------
	//  content
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _content:*;
	
	[Bindable("contentChanged")]
	[ModelBinding("org.example.ui.model.IButtonModel")]
	
	/**
	 * @copy org.example.components.IButton#content
	 */
	public function get content():*
	{
		return _content;
	}
	
	/**
	 * @private
	 */
	public function set content(value:*):void
	{
		var old:* = _content;
		_content = value;
		ToolKitUtil.dispatchChangeEvent(this, "content", old, value);
	}
	
	//----------------------------------
	//  label
	//----------------------------------
	
	[Bindable("contentChange")]
	
	/**
	 * @copy org.example.components.IButton#label
	 */
	public function get label():String
	{
		return (content != null) ? content.toString() : "";
	}
	
	/**
	 * @private
	 */
	public function set label(value:String):void 
	{
		content = value;
	}
	
	//----------------------------------
	//  icon
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _icon:Class;
	
	[Bindable("iconChanged")]
	[ModelBinding("org.example.ui.model.IButtonModel")]
	
	/**
	 * @copy org.example.components.IButton#icon
	 */
	public function get icon():Class
	{
		return _icon;
	}
	
	/**
	 * @private
	 */
	public function set icon(value:Class):void
	{
		var old:Class = _icon;
		_icon = value;
		ToolKitUtil.dispatchChangeEvent(this, "icon", old, value);
	}
	
	//----------------------------------
	//  hoverToolTip
	//----------------------------------
	
	/**
	 * 
	 */
//	public function get hoverToolTip():String
//	{
//		return super.toolTip;
//	}	
	
	/**
	 * @private
	 */
	public function set hoverToolTip(value:String):void
	{
		super.toolTip = value;
	}
	
	//----------------------------------
	//  toolTip
	//----------------------------------
	
	/**
	 * @private
	 */
	override public function set toolTip(value:String):void
	{
		super.toolTip = value;
		
		if (explicitToolTip && !value)
			invalidateDisplayList();
		
		explicitToolTip = (value != null);
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function ButtonBase()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	override protected function getCurrentSkinState():String
	{
		if (coreModel is ISkinStateAware)
			return ISkinStateAware(coreModel).getSkinState();
		
		return ButtonState.UP;
	}	
}
}